package com.n9.qqonline.sales.filter;

import com.n9.qqonline.sales.entity.Config;
import com.n9.qqonline.sales.entity.RecvMsg;

import java.util.Objects;

/**
 * 关键词过滤器
 * @author ice
 * @since 2019/3/6 19:14
 */
public class KeyWordsFilter implements RecvMsgFilter {
    private String[] keywords;

    public KeyWordsFilter(String keywordsConfig) {
        keywords = keywordsConfig.split(Config.SEPARATOR_REGEX);
    }

    @Override
    public boolean accept(RecvMsg msg) {
        String context = msg.getContent();
        if (Objects.isNull(context) || context.length() == 0) {
            return false;
        }
        if (Objects.isNull(keywords)) {
            return true;
        }
        for (int i = 0; i < keywords.length; i++) {
            if (context.contains(keywords[i])) {
                return true;
            }
        }
        return false;
    }
}
